package com.example.myapplication.government;

import androidx.appcompat.app.AppCompatActivity;

import android.content.Intent;
import android.os.Bundle;
import android.os.Handler;
import android.util.Log;
import android.view.View;
import android.widget.ImageView;
import android.widget.TextView;

import com.bumptech.glide.Glide;
import com.example.myapplication.Config.TokenBean;
import com.example.myapplication.R;
import com.example.myapplication.ShuJu.ClaimDdetailsBean;
import com.example.myapplication.ShuJu.DemandsBean;
import com.google.gson.Gson;

import org.jetbrains.annotations.NotNull;

import java.io.IOException;

import okhttp3.Call;
import okhttp3.Callback;
import okhttp3.OkHttpClient;
import okhttp3.Request;
import okhttp3.Response;

public class ClaimDdetailsActivity extends AppCompatActivity {

    private String demandApterID;
    private TextView textView;
    private ImageView imageImageView;
    private TextView contentTextView;
    private TextView departmentTextView;
    private TextView submitTimeTextView;
    private TextView statusTextView;
    private TextView resultTextView;
    private TextView titleTextView;
    private Request request;
    private OkHttpClient okHttpClient;
    private Gson gson;
    private Handler handler;
    private ClaimDdetailsBean claimDdetailsBean;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_claim_ddetails);
        Intent intent = getIntent();
        demandApterID = intent.getStringExtra("DemandApterID");
        initview();
        sendClaimDdetails();
    }

    private void initview() {
        titleTextView = findViewById(R.id.titleTextView);
        imageImageView = findViewById(R.id.imageImageView);
        contentTextView = findViewById(R.id.contentTextView);
        departmentTextView = findViewById(R.id.departmentTextView);
        submitTimeTextView = findViewById(R.id.submitTimeTextView);
        statusTextView = findViewById(R.id.statusTextView);
        resultTextView = findViewById(R.id.resultTextView);

        okHttpClient = new OkHttpClient();
        gson = new Gson();
        handler = new Handler();
    }
    private void sendClaimDdetails() {
        request =  new Request.Builder()
                .url("http://124.93.196.45:10001/prod-api/api/gov-service-hotline/appeal/"+demandApterID)
                .addHeader("Authorization", TokenBean.token)
                .build();

        okHttpClient.newCall(request).enqueue(new Callback() {
            @Override
            public void onFailure(@NotNull Call call, @NotNull IOException e) {
                e.printStackTrace();
                Log.d("xx","bb");
            }

            @Override
            public void onResponse(@NotNull Call call, @NotNull Response response) throws IOException {
                String json =  response.body().string();
                Log.d("xjson",json);
                claimDdetailsBean = gson.fromJson(json, ClaimDdetailsBean.class);
                handler.post(new Runnable() {
                    @Override
                    public void run(){
                        bindClaimDdetails();
                    }
                });
            }
        });
    }

    private void bindClaimDdetails() {
        Glide.with(ClaimDdetailsActivity.this).load(TokenBean.IP+claimDdetailsBean.getData().getImgUrl()).into(imageImageView);
        titleTextView.setText(claimDdetailsBean.getData().getTitle());
        contentTextView.setText(claimDdetailsBean.getData().getContent());
        departmentTextView.setText("单位:"+claimDdetailsBean.getData().getUndertaker());
        submitTimeTextView.setText("时间:"+claimDdetailsBean.getData().getCreateTime());
        if(claimDdetailsBean.getData().getState().equals("0")){
            resultTextView.setText("状态: 未处理");
        }else {
            resultTextView.setText("状态: 已处理");
        }

    }

}